var wx = require('weixin-js-sdk');


// 判断项目是否在wx浏览器中打开   false为微信浏览器   true为其他浏览器
window.platform = () => {
    let platform;
    if(navigator.userAgent.toLowerCase().indexOf('micromessenger') > -1 ||
        typeof navigator.wxuserAgent !== 'undefined'){
        platform = false;
    }else{
        platform = true;
    }
    return platform
}

// 截取url后面的参数
window.urlParames = (theurl = decodeURI(window.location.hash)) => {
    let theUrlcode = new Object();
    if (theurl.indexOf("?") != -1) {
        var string = theurl.substr(1);
        var stringx = string.split("?");
        var strings = stringx[1].split("&");
        for(var n = 0; n < strings.length; n ++) {
            theUrlcode[strings[n].split("=")[0]]=unescape(strings[n].split("=")[1]);
        }
    }
    return theUrlcode
}

// 截取微信回调后携带的参数
window.wxParames = () => {
    let theRequest = new Object();
    let url = decodeURI(window.location.search);
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest
}

// 微信获取分享权限
window.share = (title = '那家网', img = 'http://image.gogbuy.com/album/2019/3/26/b2b25c84-7744-4e10-b94e-ae0b6c077ffb.jpg') => {
    let config = '';
    wx.ready(function () {
        const origin = window.location.origin
        const hash = window.location.hash
        const pathname = window.location.pathname
        config = {
            title: title,
            link: origin+pathname+hash,
            imgUrl: img,
            desc: '【多彩贵州 精品黔货】',
            success: function () {
             //   alert(this.link)
            },
            cancel: function () {
            }
        }
        wx.onMenuShareTimeline(config);
        wx.onMenuShareAppMessage(config);
        wx.onMenuShareQQ(config);
        wx.onMenuShareWeibo(config);
        wx.onMenuShareQZone(config);
    });

}

/**
 * 判断浏览器版本号和内核
 * params: n获取内核, v获取版本, 不传获取内核和版本
 * */
window.getBrowser = (n) => {
    var ua = navigator.userAgent.toLowerCase(),
        s,
        name = '',
        ver = 0;
    //探测浏览器
    (s = ua.match(/msie ([\d.]+)/)) ? _set("ie", _toFixedVersion(s[1])):
        (s = ua.match(/firefox\/([\d.]+)/)) ? _set("firefox", _toFixedVersion(s[1])) :
            (s = ua.match(/chrome\/([\d.]+)/)) ? _set("chrome", _toFixedVersion(s[1])) :
                (s = ua.match(/opera.([\d.]+)/)) ? _set("opera", _toFixedVersion(s[1])) :
                    (s = ua.match(/version\/([\d.]+).*safari/)) ? _set("safari", _toFixedVersion(s[1])) : 0;

    function _toFixedVersion(ver, floatLength) {
        ver = ('' + ver).replace(/_/g, '.');
        floatLength = floatLength || 1;
        ver = String(ver).split('.');
        ver = ver[0] + '.' + (ver[1] || '0');
        ver = Number(ver).toFixed(floatLength);
        return ver;
    }
    function _set(bname, bver) {
        name = bname;
        ver = bver;
    }
    return (n == 'n' ? name : (n == 'v' ? ver : name + ver));
};

/**
 * 判断操作平台
 */
window.checkOs = () => {
    var ua = navigator.userAgent;
    var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/);
    var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
    var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
    var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);

    let os = 'web'
    if (android) {
        os = "android";
    }
    if (ipad || iphone || ipod) {
        os = "iphone";
    }
    return os;
}